package top.fullj;

import com.google.common.base.MoreObjects;
import top.fullj.bean.ToString;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * Refer to Caffeine, Guava...
 *
 * @author bruce.wu
 * @since 2021/12/1 17:08
 */
@Deprecated
public class LRULinkedHashMap<K, V> extends LinkedHashMap<K, V> {

    private static final long serialVersionUID = -8136518001494146595L;

    private final int capacity;

    public LRULinkedHashMap() {
        this(16);
    }

    public LRULinkedHashMap(int capacity) {
        super(capacity, 0.75F, true);
        this.capacity = capacity;
    }

    @Override
    protected boolean removeEldestEntry(Map.Entry<K, V> eldest) {
        return size() > capacity;
    }

    @Override
    public String toString() {
        MoreObjects.ToStringHelper toStringHelper = ToString.toStringHelper(this);
        for (Map.Entry<K, V> entry : entrySet()) {
            toStringHelper.add(String.valueOf(entry.getKey()), entry.getValue());
        }
        return toStringHelper.toString();
    }

}
